<?php
/**
 * 城市提升基金常规活动
 * @author jianhui
 *
 */
class CityProject extends MY_Controller{
    function CityProject(){
        parent::MY_Controller();
        $this->navbarFocus = 'CityBudget';
    }
    /**
     * 城市基金项目添加
     */
    function add(){
        $this->load->model('cityProjectModel');
        if ($_POST){
            $data = array(
                'storeId'       => $this->user['userId'],
                'procId'        => (int)$this->input->post('procId'),
                'isBubao'       => (int)$this->input->post('isBubao'),
                'projectName'   => $this->input->post('projectName'),
                'actionType'    => (int)$this->input->post('actionType'),
                'actionAlias'   => $this->input->post('actionAlias'),
                'eventType'     => (int)$this->input->post('eventType'),
                'eventClass'    => (int)$this->input->post('eventClass'),
                'eventClassAlias'=> $this->input->post('eventClassAlias'),
                'budgetAuditRole' => 0,
                'createTime'    => time(),
            );
            $data['storeId'] || showError('请填入店铺ID');
            $data['procId'] || showError('请选择主线流程ID');
            $data['projectName'] || showError('请输入城市基金项目名称');
            //验证标准动作
            $data['actionType'] || showError('请选择标准动作');
            $this->load->model('actionModel');
            $actionData = $this->actionModel->getData($data['actionType']);
            if (in_array($actionData['name'], array('其他','其它')) && !$data['actionAlias']){
                showError('请输入标准动作其他值');
            }
            $data['eventType'] || showError('请选择活动类型');
            $data['eventClass'] || showError('请选择活动分类');
            if($data['eventClass'] == -1 && !$data['eventClassAlias']){
                showError('请输入活动分类其他值');
            }
            $projectId = $this->cityProjectModel->add($data);
            if ($projectId){
                $this->cityProjectModel->updateMoney($projectId);
                showSuccess('添加成功', printUrl('CityProject', 'childDetail', array('procId'=>$data['procId'], 'projectId'=>$projectId)));
            }else{
                showError('添加城市基金项目失败');
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要添加城市基金项目的流程');
            $this->viewData['procId'] = $procId;
            $this->viewData['isBubao'] = (int)$this->input->get('isBubao');
            //选中状态
            $this->viewData['isBubao'] && $this->navbarFocus = 'Supplement';
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget', 'storeList'),'title'=>'城市提升基金预算申请列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget', 'storeEdit', array('procId'=>$procId)),'title'=>'城市提升基金预算编辑');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'城市基金项目添加');
            //主线流程数据
            $this->load->model('cityProcessModel');
            $procData = $this->cityProcessModel->getNewData(array('id'=>$procId));
            $this->load->model('actionModel');
            //默认基金类别为单店
            $this->viewData['actionList'] = $this->actionModel->getActiveList(3, $procData['year'],$procData['timeUnit']);
            $this->load->view('CityProject/add', $this->viewData);
        }
    }
    /**
     * 城市基金项目添加
     */
    function edit(){
        $this->load->model('cityProjectModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'       => $this->user['userId'],
                'procId'        => (int)$this->input->post('procId'),
                'isBubao'       => (int)$this->input->post('isBubao'),
                'projectName'   => $this->input->post('projectName'),
                'actionType'    => (int)$this->input->post('actionType'),
                'actionAlias'   => $this->input->post('actionAlias'),
                'eventType'     => (int)$this->input->post('eventType'),
                'eventClass'    => (int)$this->input->post('eventClass'),
                'eventClassAlias'=> $this->input->post('eventClassAlias'),
                'budgetSubTime' => time(),
                //'budgetAuditRs' => 0,
                'budgetAuditRole' => 0,
            );
            $data['storeId'] || showError('请填入店铺ID');
            $data['procId'] || showError('请选择主线流程ID');
            $data['projectName'] || showError('请输入项目名称');
            //验证标准动作
            $data['actionType'] || showError('请选择标准动作');
            $this->load->model('actionModel');
            $actionData = $this->actionModel->getData($data['actionType']);
            if (in_array($actionData['name'], array('其他','其它')) && !$data['actionAlias']){
                showError('请输入标准动作其他值');
            }
            $data['eventType'] || showError('请选择活动类型');
            $data['eventClass'] || showError('请选择活动分类');
            if($data['eventClass'] == -1 && !$data['eventClassAlias']){
                showError('请输入活动分类其他值');
            }
            $projectId = $this->cityProjectModel->edit($id,$data);
            if ($projectId){
                $this->cityProjectModel->updateMoney($projectId);
                showSuccess('修改成功', printUrl('CityProject', 'childDetail', array('procId'=>$data['procId'], 'projectId'=>$projectId)));
            }else{
                showError('修改城市基金项目失败');
            }
        }else{
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择要修改的城市基金项目');
            $this->viewData['projectId'] = $projectId;
            $projectData = $this->cityProjectModel->getNewData(array('id'=>$projectId,'storeId'=>$this->user['userId']));
            $projectData || showError('城市基金项目数据不存在');
            $this->viewData['projectData'] = $projectData;
            in_array($projectData['settleAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
            //选中状态
            if($projectData['isBubao']){
                $this->navbarFocus = 'Supplement';
                
            }
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget', 'storeList'),'title'=>'城市提升基金预算申请列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget', 'storeEdit', array('procId'=>$this->viewData['projectData']['procId'])),'title'=>'城市提升基金预算编辑');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'城市基金项目编辑');
            //主线流程数据
            $this->load->model('cityProcessModel');
            $procData = $this->cityProcessModel->getNewData(array('id'=>$this->viewData['projectData']['procId']));
            //默认基金类别为单店
            $this->load->model('actionModel');
            $this->viewData['actionList'] = $this->actionModel->getActiveList(3, $procData['year'],$procData['timeUnit']);
            $this->load->view('CityProject/edit', $this->viewData);
        }
    }
    /**
     * 城市基金项目删除
     */
    function del(){
        $projectId = (int)$this->input->post('projectId');
        $projectId || showError('请选择要删除的城市基金项目ID');
        $this->load->model('cityProjectModel');
        if ($projectId){
            $this->load->model('cityODetailModel');
            $this->cityODetailModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->load->model('cityOfflineModel');
            $this->cityOfflineModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->load->model('cityGxModel');
            $this->cityGxModel->del(array('projectId'=>$projectId,'storeId'=>$this->user['userId']),'*');
            $this->cityProjectModel->del(array('id'=>$projectId,'storeId'=>$this->user['userId']) );
            showSuccess('删除城市基金项目成功');
        }else{
            showError('删除城市基金项目失败');
        }
    }
    /**
     * 城市基金项目子类详细，用于编辑信息
     */
    function childDetail(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择城市基金项目');
        $this->viewData['projectId'] = $projectId;
        //城市基金项目模型
        $this->load->model('cityProjectModel');
        $projectData = $this->cityProjectModel->getNewData(array('id'=>$projectId,'storeId'=>$this->user['userId']));
        $projectData || showError('城市基金项目数据不存在');
        $this->viewData['projectData'] = $projectData;
        //选中状态
        $projectData['isBubao'] && $this->navbarFocus = 'Supplement';
        $this->viewData['isBubao'] = $projectData['isBubao'];
        //金额明细
        $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('cityGxModel');
        $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //线下活动
        $this->load->model('cityOfflineModel');
        $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget'.($projectData['isBubao']?'Again':''), 'storeList'),'title'=>'城市提升基金预算申请列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('CityBudget'.($projectData['isBubao']?'Again':''), 'storeEdit', array('procId'=>$procId)),'title'=>'城市提升基金预算编辑');
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'城市基金项目编辑');
        
        $this->viewData['action'] = 'edit';
        $this->load->view('CityProject/childDetail', $this->viewData);
    }
    /**
     * 预算查看
     */
    function budgetCheck(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择城市基金项目');
        $this->viewData['projectId'] = $projectId;
        //城市基金项目模型
        $this->load->model('cityProjectModel');
        $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
        $this->viewData['projectData'] || showError('城市基金项目数据不存在');
        $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('cityGxModel');
        $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //线下活动
        $this->load->model('cityOfflineModel');
        $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId));
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('CityProject/budgetCheck', $this->viewData);
    }
    /**
     * 城市基金项目预算提交(此功能暂时在城市提升基金中不需要)
     */
    function budgetSubmit(){
        $projectId = (int)$this->input->post('projectId');
        $projectId || showError('请选择要提交预算申请的城市基金项目');
        $this->load->model('cityProjectModel');
        $projectData = $this->cityProjectModel->getData(array('id'=>$projectId, 'storeId'=>$this->user['userId']));
        $projectData || showError('城市基金项目数据不存在');
        if ($this->cityProjectModel->edit($projectId, array('budgetAuditRs'=>0,'budgetSubTime'=>time()))){
            $this->load->model('cityGxModel');
            $this->load->model('cityOfflineModel');
            $this->cityGxModel->edit(array('projectId'=>$projectId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0), '*');
            $this->cityOfflineModel->edit(array('projectId'=>$projectId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0), '*');
            //更新金额
            $this->cityProjectModel->updateMoney($projectId);
            showSuccess('提交城市基金项目预算申请成功', printUrl('CityBudget'.($projectData['isBubao']?'Again':''), 'storeEdit', array('procId'=>$projectData['procId'])));
        }else{
            showError('提交城市基金项目预算申请失败');
        }
    }
    /**
     * 预算审核(此功能暂时在城市提升基金中不需要)
     */
    function budgetAudit(){
        $this->load->model('cityProcessModel');
        $this->load->model('cityProjectModel');
        $this->load->model('cityGxModel');
        $this->load->model('cityOfflineModel');
        if ($_POST){
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择项目');
            $projectData = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            if ($projectData['settleAuditRs'] > -1){
                showError('项目已申请结算,无法进行预算审核');
            }
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('请还未选择审核项');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->cityGxModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->cityOfflineModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                }
            }
            $this->cityProjectModel->edit($projectId, array('budgetAuditDetail'=>1,'budgetAuditRole'=>$this->user['userRole']));
            $this->cityProjectModel->budgetUpdateStatus($projectId);
            $this->cityProcessModel->budgetUpdateStatus($projectData['procId']);
            //更新城市基金项目金额
            $this->cityProjectModel->updateMoney($projectId);
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择城市基金项目');
            $this->viewData['projectId'] = $projectId;
            //城市基金项目模型
    
            $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('城市基金项目数据不存在');
            $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetApply >'=>0));
            //线下活动
            $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetApply >'=>0));
            //查看标记
            $this->viewData['action'] = 'audit';
            $this->load->view('CityProject/budgetAudit', $this->viewData);
        }
    }
    /**
     * 结算申请
     */
    function settleSubmit(){
        $this->load->model('cityProjectModel');
        $this->load->model('cityGxModel');
        $this->load->model('cityOfflineModel');
        if ($_POST){
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('您的结算申请数据为空');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    if ((int)$v['isSettle']){
                        (float)$v['settleApply'] >= 0 || showError('编号为【'.$k.'】的广宣实际费用为空');
                        (int)$v['fileId'] || showError('编号为【'.$k.'】的广宣自证照片为空');
                    }
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    if ((int)$v['isSettle']){
                        (float)$v['settleApply'] >= 0 || showError('编号为【'.$k.'】的线下活动实际费用为空');
                        (int)$v['fileId'] || showError('编号为【'.$k.'】的线下活动自证照片为空');
                    }
                }
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->cityGxModel->edit((int)$k, array(
                        'settleAuditRole' => 0,
                        'settleAuditRs'=>(int)$v['isSettle'] ? 0 : -1,
                        'isSettle'=>(int)$v['isSettle'],
                        'fileId' => (int)$v['fileId'],
                        'settleApply' => (float)$v['settleApply'],
                        'settleSubTime' => time()
                    ));
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->cityOfflineModel->edit((int)$k, array(
                        'settleAuditRole' => 0,
                        'settleAuditRs'=>(int)$v['isSettle'] ? 0 : -1,
                        'isSettle'=>(int)$v['isSettle'],
                        'fileId' => (int)$v['fileId'],
                        'settleApply' => (float)$v['settleApply'],
                        'settleSubTime' => time()
                    ));
                }
            }
            $projectId = (int)$this->input->post('projectId');
            $this->cityProjectModel->edit($projectId, array(
                'settleAuditDetail' => 0,
                'settleSubTime'=>time(),
            ));
            $this->cityProjectModel->settleUpdateStatus($projectId);
            $this->cityProjectModel->updateMoney($projectId);
            showSuccess('提交结算申请成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择城市基金项目');
            $this->viewData['projectId'] = $projectId;
            //城市基金项目模型
            $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('城市基金项目数据不存在');
            $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //线下活动
            $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //查看标记
            $this->viewData['action'] = 'edit';
            $this->load->view('CityProject/settleSubmit', $this->viewData);
        }
    }
    /**
     * 预算查看
     */
    function settleCheck(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择主线序号');
        $this->viewData['procId'] = $procId;
        $projectId = (int)$this->input->get('projectId');
        $projectId || showError('请选择城市基金项目');
        $this->viewData['projectId'] = $projectId;
        //城市基金项目模型
        $this->load->model('cityProjectModel');
        $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
        $this->viewData['projectData'] || showError('城市基金项目数据不存在');
        $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
        //线上广宣
        $this->load->model('cityGxModel');
        $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
        //线下活动
        $this->load->model('cityOfflineModel');
        $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('CityProject/settleCheck', $this->viewData);
    }
    /**
     * 结算审核
     */
    function settleAudit(){
        $this->load->model('cityProcessModel');
        $this->load->model('cityProjectModel');
        $this->load->model('cityGxModel');
        $this->load->model('cityOfflineModel');
        if ($_POST){
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择要审核的城市基金项目');
            $projectData = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('请还未选择审核项');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->cityGxModel->settleAudit((int)$k, (int)$v['settleAuditRs'], $this->user);
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->cityOfflineModel->settleAudit((int)$k, (int)$v['settleAuditRs'], $this->user);
                }
            }
            $this->cityProjectModel->edit($projectId, array('settleAuditDetail'=>1,'settleAuditRole'=>$this->user['userRole']));
            $this->cityProjectModel->settleUpdateStatus($projectId);
            $this->cityProcessModel->settleUpdateStatus($projectData['procId']);
            //更新城市基金项目金额
            $this->cityProjectModel->updateMoney($projectId);
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择城市基金项目');
            $this->viewData['projectId'] = $projectId;
            //城市基金项目模型
            $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('城市基金项目数据不存在');
            $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //线下活动
            $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'isSettle'=>1,'settleApply >'=>0));
            //查看标记
            $this->viewData['action'] = 'audit';
            $this->load->view('CityProject/settleAudit', $this->viewData);
        }
    }
    /**
     * 修改金额
     */
    function settleChange(){
        $this->load->model('cityProjectModel');
        $this->load->model('cityGxModel');
        $this->load->model('cityOfflineModel');
        if ($_POST){
            $projectId = (int)$this->input->post('projectId');
            $projectId || showError('请选择项目');
            $gxData = $this->input->post('gx');
            $offlineData = $this->input->post('offline');
            if (!$gxData && !$offlineData){
                showError('您的提交数据为空');
            }
            if ($gxData){
                foreach ($gxData as $k => $v){
                    $this->cityGxModel->edit((int)$k, array(
                        'settleApply' => (float)$v['settleApply'],
                        'settleAudit' => (int)$v['settleAuditRs'] == 1 ? (float)$v['settleApply'] : 0,
                    ));
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->cityOfflineModel->edit((int)$k, array(
                        'settleApply' => (float)$v['settleApply'],
                        'settleAudit' => (int)$v['settleAuditRs'] == 1 ? (float)$v['settleApply'] : 0,
                    ));
                }
            }
            $this->cityProjectModel->settleUpdateStatus($projectId);
            $this->cityProjectModel->updateMoney($projectId);
            showSuccess('保存成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择主线序号');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $projectId || showError('请选择城市基金项目');
            $this->viewData['projectId'] = $projectId;
            //城市基金项目模型
            $this->viewData['projectData'] = $this->cityProjectModel->getNewData(array('id'=>$projectId));
            $this->viewData['projectData'] || showError('城市基金项目数据不存在');
            $this->viewData['moneyDetail'] = $this->cityProjectModel->getDetailMoney($projectId);
            //线上广宣
            $this->viewData['gxList'] = $this->cityGxModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //线下活动
            $this->viewData['offlineList'] = $this->cityOfflineModel->getNewList(array('projectId'=>$projectId,'procId'=>$procId,'budgetAuditRs'=>1));
            //查看标记
            $this->viewData['action'] = 'change';
            $this->load->view('CityProject/settleSubmit', $this->viewData);
        }
    }
}